package com.icarsaas.saas.tenant.cars.customer.web;

import com.icarsaas.saas.common.enums.StatusEnum;
import com.icarsaas.saas.common.page.SoulPage;
import com.icarsaas.saas.common.utils.ResultVoUtil;
import com.icarsaas.saas.common.utils.StatusUtil;
import com.icarsaas.saas.common.vo.ResultVo;
import com.icarsaas.saas.component.actionLog.action.SaveAction;
import com.icarsaas.saas.component.actionLog.action.StatusAction;
import com.icarsaas.saas.component.actionLog.annotation.ActionLog;
import com.icarsaas.saas.component.actionLog.annotation.EntityParam;
import com.icarsaas.saas.tenant.cars.customer.model.Coupon;
import com.icarsaas.saas.tenant.cars.customer.service.CouponService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @title: CouponController
 * @description: 优惠券管理
 * @author xrong120@gmail.com
 * @date 2021年1月23日
 */
@Api(tags="优惠券模块")
@Controller @RequestMapping(value = "/cars/coupon")
public class CouponController {
	@Autowired private CouponService couponService;
	
	@GetMapping("/couponList")
	public String couponList() {
		return "/base/couponList";
	}

	@PostMapping("listSoulTable") @ResponseBody
	public Object listSoulTable(SoulPage<Map<String, Object>> page, @RequestBody Map<String, Object> params) {
		page.setParams(params, page);
		page = couponService.listSoulData(page, params);
		return page;
	}


	@ModelAttribute
	public Coupon get(@RequestParam(required = false) String id) {
		Coupon entity = null;
		if (id != null) entity = couponService.get(id);
		else entity = new Coupon();
		return entity;
	}
	/**
	 * 跳转到编辑页面
	 */
	@GetMapping({"couponForm", "couponForm/{id}"})
	public String couponForm(@RequestParam(value = "id", required = false) Coupon entity, Model model) {
		if (entity != null) model.addAttribute("entity", entity);
		return "/base/couponForm";
	}
	/**
	 * 保存添加/修改的数据
	 */
	@PostMapping({"/saveEntity"}) @ResponseBody
	@ActionLog(name = "优惠券管理", message = "优惠券信息：${name}", action = SaveAction.class)
	public ResultVo<?> saveEntity(@EntityParam Coupon entity) {
		couponService.save(entity);
		return ResultVoUtil.SAVE_SUCCESS;
	}

	/**
	 * 删除记录
	 * @param ids 删除的数据id
	 * @param param 状态标识，必须带
	 * @return
	 */
	@PostMapping("/deleteEntity") @ResponseBody
	@ActionLog(name = "优惠券管理", message = "删除优惠券信息：${name}", action = StatusAction.class)
	public ResultVo<?> deleteEntity(@RequestParam(value = "ids", required = false) List<String> ids
			, @RequestParam(value = "param", required = false) String param) {
		// 更新状态
		if (ids != null && ids.size() > 0) {
			ids.forEach(id -> {
				couponService.delete(id);
			});
			return ResultVoUtil.success("操作成功");
		} else {
			return ResultVoUtil.error("操作失败，请重试!");
		}
	}

	/**
	 * 跳转到详细页面
	 */
	@GetMapping("/detail/{id}")
	public String toDetail(@PathVariable("id") Coupon entity, Model model){
		model.addAttribute("entity", entity);
		return "/base/couponDetail";
	}

	/**
	 * 设置一条或者多条数据的状态
	 */
	@PostMapping("status") @ResponseBody
	@ActionLog(name = "优惠券状态", action = StatusAction.class)
	public ResultVo<?> updateStatus(@RequestParam(value = "ids", required = false) List<String> ids
			, @RequestParam(value = "param", required = false) String param) {
		// 更新状态
		StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
		if (couponService.updateStatus(statusEnum, ids)) {
			return ResultVoUtil.success(statusEnum.getMessage() + "成功");
		} else {
			return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
		}
	}
}
